//
//  MYNewsRankCollViewCell.m
//  MuYing
//
//  Created by tbb on 2019/8/5.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYNewsRankCollViewCell.h"
#import "MYNewsAllRankVC.h"
#import "MYShopDetailsVC.h"
#import "MYNewsRankHomeModel.h"
#import "MYNewsRankModel.h"
@interface MYNewsRankCollViewCell ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) YQBaseTableView * rankTableView;


@end

@implementation MYNewsRankCollViewCell

-(void)setRankingModel:(MYNewsRankRankingModel *)rankingModel {
    _rankingModel = rankingModel;

    [self.rankTableView reloadData];
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.contentView.layer.cornerRadius = 44;
    self.contentView.clipsToBounds = true;
    self.contentView.layer.borderWidth = 7;
    self.contentView.layer.borderColor = kAPPThemeColor.CGColor;
    
    [self.contentView addSubview:self.rankTableView];

}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.rankingModel.rankingProductList.count > 3) {
        return 3;
    }
    return self.rankingModel.rankingProductList.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYNewsRankCollViewTabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsRankCollViewTabelCell"];
    MYNewsRankRankingProductModel *productModel = nil;
    if (indexPath.row < self.rankingModel.rankingProductList.count) {
        productModel = self.rankingModel.rankingProductList[indexPath.row];
    }
    cell.goodsTitleLb.text = productModel.skuName;
    cell.commentCountLb.text = [NSString stringWithFormat:@"%@%@",productModel.commentNumber,kYQLS(@"位用户评论")];
    [cell.goodsIcon sd_setImageWithURL:[NSURL URLWithString:productModel.defaultImage]];
    cell.lineView.hidden = YES;
    [cell.rankBtn setTitle:[NSString stringWithFormat:@"%zd",indexPath.row+1] forState:(UIControlStateNormal)];
    cell.starRateView.currentStarRate = productModel.score;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 112;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MYShopDetailsVC *detailsVC = [[MYShopDetailsVC alloc] init];
    MYNewsRankRankingProductModel *productModel = nil;
    if (indexPath.row < self.rankingModel.rankingProductList.count) {
        productModel = self.rankingModel.rankingProductList[indexPath.row];
    }
    detailsVC.skuId = productModel.skuId;
    [self.YQViewController.navigationController pushViewController:detailsVC animated:YES];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    MYNewsRankCollViewFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYNewsRankCollViewFooter"];
    ws(self);
    [footer.lookBtn addTapAction:^(id  _Nonnull sender) {
        MYNewsAllRankVC *rankVC = [[MYNewsAllRankVC alloc] init];
        rankVC.rankingModel = weakself.rankingModel;
        rankVC.rankHomemModel = self.rankHomemModel;
        [weakself.YQViewController.navigationController pushViewController:rankVC animated:true];
    }];
    return footer;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 72;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYNewsRankCollViewHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYNewsRankCollViewHeader"];
    header.titleLb.text = self.rankingModel.rankingName;
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 67;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

#pragma makr -- lazy
-(YQBaseTableView *)rankTableView {
    if (!_rankTableView) {
        _rankTableView = [[YQBaseTableView alloc] initWithFrame:self.contentView.bounds style:(UITableViewStyleGrouped)];
        _rankTableView.backgroundColor = UIColor.whiteColor;
        _rankTableView.dataSource = self;
        _rankTableView.delegate = self;
        [_rankTableView registerNib:[UINib nibWithNibName:@"MYNewsRankCollViewHeader" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYNewsRankCollViewHeader"];
        [_rankTableView registerNib:[UINib nibWithNibName:@"MYNewsRankCollViewFooter" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MYNewsRankCollViewFooter"];
        [_rankTableView registerNib:[UINib nibWithNibName:@"MYNewsRankCollViewTabelCell" bundle:nil] forCellReuseIdentifier:@"MYNewsRankCollViewTabelCell"];
    }
    return _rankTableView;
}
#pragma makr -- lazy end

@end
